package cn.iocoder.yudao.module.infrastructure.notice.controller.admin;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infrastructure.notice.controller.admin.vo.NoticeRespVO;
import cn.iocoder.yudao.module.infrastructure.notice.dal.dataobject.NoticeDO;
import cn.iocoder.yudao.module.infrastructure.notice.dal.mysql.NoticeMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/12/12
 * @see NoticeController
 */
@Tag(name = "管理后台 - 通知公告")
@RestController
@RequestMapping("/system/notice")
@Validated
public class NoticeV2Controller {

    @Resource
    private NoticeMapper noticeMapper;

    @GetMapping("/list")
    @Operation(summary = "获取通知公告列表")
    @Parameter(name = "limit", description = "数量", required = true, example = "10")
    @PermitAll
    public CommonResult<List<NoticeRespVO>> getNoticeList(@RequestParam("limit") Integer limit) {
        if (limit == null || limit <= 0) {
            limit = 10;
        }
        CoreLambdaQueryWrapper<NoticeDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(NoticeDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
        wrapper.limitN(limit);
        wrapper.orderByDesc(NoticeDO::getId);
        List<NoticeDO> list = noticeMapper.selectList(wrapper);
        return success(BeanUtils.toBean(list, NoticeRespVO.class));
    }

}
